<?php
class Country_Controller extends AdminController {

    public $DISPLAY_PERSON_LIST_UUID = 'admin/country/display/personList';

    public function __construct() {
        parent::__construct('country');
        $this->setDefaultListOrder('name');
    }
    
    function all() {
        $this->initPagination('country');
        
        $this->viewData['countryList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM country'));
        $this->renderSubview('admin/country/countryList');
    }
    
    function display() {
        $code = $this->input->get('code');

        if (! $code) {
            $this->redirectToList('error.invalidId');
        }

        $country = $this->dao->getByCode('country', $code);
        if (! $country) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['country'] = $country;

        if (! request::is_ajax()) {
            // Persons from this country
            $this->viewData['personList'] = $this->dao->queryAll(sql::orderBy($this->DISPLAY_PERSON_LIST_UUID,
                    'SELECT * FROM person WHERE country_code = :country_code', 'last_name'),
                    array('country_code' => $code));
        }
        $this->renderSubview('admin/country/countryDisplay');
    }
    
    function edit() {
        $code = $this->input->get('code');
        $country = array();
        
        if ($code) {
            $country = $this->dao->getByCode('country', $code);
            if (! $country) {
                $this->redirectToList('error.recordNotFound');
            }
        }
        else {
            // Set default insert values here
        }
        
        $this->viewData['country'] = $country;
        $this->renderSubview('admin/country/countryEdit');
    }
    
    function save() {
        $country = $this->postToDbValues();
        $code = $country['code'];
        $this->viewData['country'] = $country;
        
        // Basic validation
        $validator = new EntityValidator($country, 'country', $this->msg);
        $validator->required('code');
        $validator->required('name');
        $validator->required('name_native');
        
        if ($this->msg->hasErrors()) {
            $this->publishErrors('admin/country/countryEdit');
            return;
        }

        // Database validation
        if ($this->dao->alreadyExists('country', 'name', $country, 'code')) {
            $this->msg->error('country.error-alreadyExists-name', $country['name']);
        }
        if (! sql::isPersistent($country)) {
            $existingObject = $this->dao->getByCode('country', $code);
            if ($existingObject) {
                $this->msg->error('country.error-alreadyExists-code', $country['code']);
            }
        }
        if ($this->msg->hasErrors()) {
            $this->renderSubview('admin/country/countryEdit');
            return;
        }

        // Insert / Update
        if (sql::isPersistent($country)) {
            $this->dao->update('country', $country, 'code');
            $this->msg->info('country.editSuccess', $country['name']);
        }
        else {
            $this->dao->insert('country', $country);
            $this->msg->info('country.createSuccess', $country['name']);
        }
        
        $this->ajaxSuccessOrRedirect('/admin/country/all', $country);
    }
    
    function delete() {
        $code = $this->input->get('code');
        
        // Basic validation
        if (! valid::isCode($code)) {
            $this->redirectToList('error.invalidId');
        }

        $country = $this->dao->getByCode('country', $code);
        if (! $country) {
            $this->redirectToList('error.recordNotFound');
        }
        
        $this->dao->deleteByCode('country', $code);
        
        $this->msg->info('country.deleteSuccess', $country['name']);
        $this->redirectToList();
    }
}
?>
